<?php
/* SVN FILE: $Id: app_helper.php 7945 2008-12-19 02:16:01Z gwoo $ */
/**
 * Short description for file.
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake
 * @since         CakePHP(tm) v 0.2.9
 * @version       $Revision: 7945 $
 * @modifiedby    $LastChangedBy: gwoo $
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Core', 'Helper');
/**
 * This is a placeholder class.
 * Create the same file in app/app_helper.php
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.cake
 */
class AppHelper extends Helper {
	var $helpers = array('Form');
	
	/*
	 * name
	 * path
	 * field_name
	 * label
	 * delete_image_link
	 * legend_title
	 */
	function form_image($options,$htmlAttributes,$image_height = null) {
		$retVal = "";
		if (is_file($options['path'].$options['name'])) {
			$retVal = "<div class='image_preview_container' style='padding-top:10px'>
			<img src='".$this->Form->url('/'.$options['path'].$options['name'])."' height='100' />";
			
			if (isset($options['delete_image_link'])) {
				$retVal .= "<span>[ <a href='".$options['delete_image_link']."'>".__('Xóa hình này',true)."</a> ]</span>";
			}
			$retVal .= "</div>";
		}
		$htmlAttributes['name'] = 'FileField['.$options['field_name'].']';
		$htmlAttributes['type'] = 'file';
		$htmlAttributes['label'] = $options['label'];
		$htmlAttributes['class'] = 'file';
		$htmlAttributes['div'] = true;
		
		$retVal .= $this->Form->input('FileField', $htmlAttributes);
		
		if (isset($options['legend_title'])) {
			return "<fieldset class='image_set list-table' >
			<legend>".$options['legend_title'].":</legend>
				".$retVal."
			</fieldset>";
		} else
			return $retVal;
	}
	
	/**
	 * Phương thức giúp bổ sung named params vào url
	 * @param $url
	 * @param $showNamedArgs
	 * @param $full
	 * @return unknown_type
	 */
	function url($url = null, $showNamedArgs = true, $full = false) {
		//if ($showNamedArgs && is_array($this->params['named']) && is_array($url)) {
		//	$url = $this->params['named'] + $url;
		//}
		return 
			//'/' .
		   //Configure::read('Config.langCode') .
		   parent::url($url, $full);
	}
	
	/*
	 * Phương thức được sử dụng trong trường hợp root của ứng dụng nằm trong subfolder
	 * */
	function webroot($url) {
		if (!is_array($url)) {
			return parent::webroot(Configure::read('App.baseUrl').$url);
		} else {
			return parent::webroot($url);
		}
	}
	
	/*
	 * Phương thức được sử dụng để đưa ra chọn lựa ngôn ngữ trên website
	 * */
	function lang_selector() {
		$retVal = "";
		$url = $this->params['url']['url'];
		if (substr($url,0,1) == "/")
			$url = substr($url,1);
		$languages = Configure::read('Config.languages');
		foreach ($languages as $langCode => $language) {
			$retVal .= "<a href='".$this->base."/".$langCode."/".$url."'><img src='".$this->webroot."img/flags/".$language.".png' border='0'/></a>\n";
		}
		return $retVal;
	}
	
	/*
	 * Phương thức được sử dụng để set class cho menu khi menu đó được chọn
	 * */
	function menuSelected($params, $test = false) {
		foreach ($params as $key => $item) {
			if (is_numeric($key)) {
				if (!in_array($item, $this->params['pass'])) {
					return '';
				}
			} else {
				if (!array_key_exists($key, $this->params) // Key không tồn tại 
					|| (!is_array($item) && $item != $this->params[$key]) // Key tồn tại, nhưng không phù hợp với params 
					) {
					return '';
				}
			}
		}
		return 'current';
	}
	/*
	 * Phương thức sử dụng để set class cho menu khi menu đó được chọn dựa vào contronller
	 */
	function menuSelected_top($controller) {
		if ($this->params['controller'] == $controller) {
			return 'current';
		} else {
			return '';
		}
	}
	function getThumbnailName($image) {
		$ext = strrchr($image, ".");
		$pos = strrpos($image, ".");
		$filename = substr($image,0,$pos);
		$image = $filename."_thumbnail".$ext;
		return $image;
	}
	function getCropName($image) {
		$ext = strrchr($image, ".");
		$pos = strrpos($image, ".");
		$filename = substr($image,0,$pos);
		$image = $filename."_crop".$ext;
		return $image;
	}
}
?>